package com.example.wjh.controller;

import com.example.wjh.entity.Admin;
import com.example.wjh.entity.Coach;
import com.example.wjh.entity.Player;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.AdminMapper;
import com.example.wjh.mapper.CoachMapper;
import com.example.wjh.mapper.PlayerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private PlayerMapper playerMapper;
    @Autowired
    private CoachMapper coachMapper;

    //管理员登录
    @PostMapping("/admin")
    public Result loginAdmin(@RequestBody Admin admin){
        Admin a = adminMapper.login(admin.getUsername(), admin.getPassword());
        if (a != null) {
            return Result.success(a.getName()); // 或者 Result.success(a);
        } else {
            return Result.error("账号或密码错误");
        }
    }

    //球员登录
    @PostMapping("/player")
    public Result loginPlayer(@RequestBody Player player){
        Player p = playerMapper.login(player);
        if (p != null) {
            return Result.success(p.getName()); // 或者 Result.success(p);
        } else {
            return Result.error("账号或密码错误");
        }
    }

    //教练登录
    @PostMapping("/coach")
    public Result loginCoach(@RequestBody Coach coach){
        Coach c = coachMapper.login(coach.getUsername(), coach.getPassword());
        if (c != null) {
            return Result.success(c.getName()); // 或者 Result.success(c);
        } else {
            return Result.error("账号或密码错误");
        }
    }
}
